home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / perl5 / XML / SAX2Perl.pm < prev    next >
Encoding:
Text File  |  2003-10-21  |  6.3 KB  |  262 lines

  1. #
  2. # Copyright (C) 1998 Ken MacLeod
  3. # XML::SAX2Perl is free software; you can redistribute it and/or
  4. # modify it under the same terms as Perl itself.
  5. #
  6. # $Id: SAX2Perl.pm,v 1.4 2001/07/23 15:47:15 kmacleod Exp $
  7. #
  8.  
  9. use strict;
  10.  
  11. package XML::SAX2Perl;
  12.  
  13. use vars qw{ $VERSION };
  14.  
  15. # will be substituted by make-rel script
  16. $VERSION = "0.08";
  17.  
  18. sub new {
  19.     my $type = shift;
  20.     my $self = ($#_ == 0) ? shift : { @_ };
  21.  
  22.     return bless $self, $type;
  23. }
  24.  
  25. sub setDocumentLocator {
  26.     my $self = shift;
  27.     my $self->{Locator} = shift;
  28. }
  29.  
  30. sub startDocument {
  31.     my $self = shift;
  32.  
  33.     my @properties;
  34.     if (defined $self->{Locator}) {
  35.     push @properties, locator => $self->{Locator};
  36.     }
  37.  
  38.     $self->{DocumentHandler}->start_document(@properties);
  39. }
  40.  
  41. sub endDocument {
  42.     my $self = shift;
  43.  
  44.     $self->{DocumentHandler}->end_document;
  45. }
  46.  
  47. sub startElement {
  48.     my $self = shift;
  49.     my $name = shift;
  50.     my $attributes = shift;
  51.  
  52.     # FIXME depends on how Perl SAX treats attributes
  53.     $self->{DocumentHandler}->start_element(Name => $name, Attributes => $attributes);
  54. }
  55.  
  56. sub endElement {
  57.     my $self = shift;
  58.     my $name = shift;
  59.  
  60.     $self->{DocumentHandler}->end_element(Name => $name);
  61. }
  62.  
  63. sub characters {
  64.     my $self = shift;
  65.     my $ch = shift;
  66.     my $start = shift;
  67.     my $length = shift;
  68.  
  69.     $self->{DocumentHandler}->characters(Data => substr($ch, $start, $length));
  70. }
  71.  
  72. sub ignorableWhitespace {
  73.     my $self = shift;
  74.     my $ch = shift;
  75.     my $start = shift;
  76.     my $length = shift;
  77.  
  78.     $self->{DocumentHandler}->ignorable_whitespace(Data => substr($ch, $start, $length));
  79. }
  80.  
  81. sub processingInstruction {
  82.     my $self = shift;
  83.     my $target = shift;
  84.     my $data = shift;
  85.  
  86.     $self->{DocumentHandler}->processing_instruction(Target => $target, Data => $data);
  87. }
  88.  
  89. 1;
  90.  
  91. __END__
  92.  
  93. =head1 NAME
  94.  
  95. XML::SAX2Perl -- translate Java/CORBA style SAX methods to Perl methods
  96.  
  97. =head1 SYNOPSIS
  98.  
  99.  use XML::SAX2Perl;
  100.  
  101.  $sax2perl = XML::SAX2Perl(Handler => $my_handler);
  102.  $sax->setDocumentHandler($sax2perl);
  103.  
  104. =head1 DESCRIPTION
  105.  
  106. C<XML::SAX2Perl> is a SAX filter that translates Java/CORBA style SAX
  107. methods to Perl style method calls.  This man page summarizes the
  108. specific options, handlers, and properties supported by
  109. C<XML::SAX2Perl>; please refer to the Perl SAX standard C<XML::SAX>
  110. for general usage information.
  111.  
  112. =head1 METHODS
  113.  
  114. =over 4
  115.  
  116. =item new
  117.  
  118. Creates a new parser object.  Default options for parsing, described
  119. below, are passed as key-value pairs or as a single hash.  Options may
  120. be changed directly in the parser object unless stated otherwise.
  121. Options passed to `C<parse()>' override the default options in the
  122. parser object for the duration of the parse.
  123.  
  124. =item parse
  125.  
  126. Parses a document.  Options, described below, are passed as key-value
  127. pairs or as a single hash.  Options passed to `C<parse()>' override
  128. default options in the parser object.
  129.  
  130. =item location
  131.  
  132. Returns the location as a hash:
  133.  
  134.   ColumnNumber    The column number of the parse.
  135.   LineNumber      The line number of the parse.
  136.   PublicId        A string containing the public identifier, or undef
  137.                   if none is available.
  138.   SystemId        A string containing the system identifier, or undef
  139.                   if none is available.
  140.  
  141. =item SAX DocumentHandler Methods
  142.  
  143. The following methods are DocumentHandler methods that the SAX 1.0
  144. parser will call and C<XML::SAX2Perl> will translate to Perl SAX
  145. methods calls.  See SAX 1.0 for details.
  146.  
  147.  setDocumentLocator(locator)
  148.  startDocument()
  149.  endDocument()
  150.  startElement(name, atts)
  151.  endElement(name)
  152.  characters(ch, start, length)
  153.  ignorableWhitespace(ch, start, length)
  154.  processingInstruction(target, data)
  155.  
  156. =back
  157.  
  158. =head1 OPTIONS
  159.  
  160. The following options are supported by C<XML::SAX2Perl>:
  161.  
  162.  Handler          default handler to receive events
  163.  DocumentHandler  handler to receive document events
  164.  DTDHandler       handler to receive DTD events
  165.  ErrorHandler     handler to receive error events
  166.  EntityResolver   handler to resolve entities
  167.  Locale           locale to provide localisation for errors
  168.  Source           hash containing the input source for parsing
  169.  
  170. If no handlers are provided then all events will be silently ignored,
  171. except for `C<fatal_error()>' which will cause a `C<die()>' to be
  172. called after calling `C<end_document()>'.
  173.  
  174. If a single string argument is passed to the `C<parse()>' method, it
  175. is treated as if a `C<Source>' option was given with a `C<String>'
  176. parameter.
  177.  
  178. The `C<Source>' hash may contain the following parameters:
  179.  
  180.  ByteStream       The raw byte stream (file handle) containing the
  181.                   document.
  182.  String           A string containing the document.
  183.  SystemId         The system identifier (URI) of the document.
  184.  PublicId         The public identifier.
  185.  Encoding         A string describing the character encoding.
  186.  
  187. If more than one of `C<ByteStream>', `C<String>', or `C<SystemId>',
  188. then preference is given first to `C<ByteStream>', then `C<String>',
  189. then `C<SystemId>'.
  190.  
  191. =head1 HANDLERS
  192.  
  193. The following handlers and properties are supported by
  194. C<XML::SAX2Perl>:
  195.  
  196. =head2 DocumentHandler methods
  197.  
  198. =over 4
  199.  
  200. =item start_document
  201.  
  202. Receive notification of the beginning of a document. 
  203.  
  204.  Locator          An object that can return the location of any SAX
  205.                   document event.
  206.  
  207. =item end_document
  208.  
  209. Receive notification of the end of a document. 
  210.  
  211. No properties defined.
  212.  
  213. =item start_element
  214.  
  215. Receive notification of the beginning of an element. 
  216.  
  217.  Name             The element type name.
  218.  Attributes       Attributes attached to the element, if any.
  219.  
  220. ALPHA WARNING: The `C<Attributes>' value is not translated from the
  221. SAX 1.0 value, so it will contain an AttributeList object.
  222.  
  223. =item end_element
  224.  
  225. Receive notification of the end of an element. 
  226.  
  227.  Name             The element type name.
  228.  
  229. =item characters
  230.  
  231. Receive notification of character data. 
  232.  
  233.  Data             The characters from the XML document.
  234.  
  235. =item ignorable_whitespace
  236.  
  237. Receive notification of ignorable whitespace in element content. 
  238.  
  239.  Data             The characters from the XML document.
  240.  
  241. =item processing_instruction
  242.  
  243. Receive notification of a processing instruction. 
  244.  
  245.  Target           The processing instruction target. 
  246.  Data             The processing instruction data, if any.
  247.  
  248. =back
  249.  
  250. =head1 AUTHOR
  251.  
  252. Ken MacLeod <ken@bitsko.slc.ut.us>
  253.  
  254. =head1 SEE ALSO
  255.  
  256. perl(1), XML::Perl2SAX(3).
  257.  
  258.  Extensible Markup Language (XML) <http://www.w3c.org/XML/>
  259.  Simple API for XML (SAX) <http://www.megginson.com/SAX/>
  260.  
  261. =cut
  262.